package com.ruoyi.system.service.aggregation.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.aggregation.Account;
import com.ruoyi.system.domain.aggregation.MerchantAudit;
import com.ruoyi.system.domain.aggregation.User;
import com.ruoyi.system.mapper.aggregation.AccountMapper;
import com.ruoyi.system.mapper.aggregation.MerchantAuditMapper;
import com.ruoyi.system.mapper.aggregation.UserMapper;
import com.ruoyi.system.service.aggregation.IMerchantAuditService;

/**
 * 商家审核Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-01
 */
@Service
public class MerchantAuditServiceImpl implements IMerchantAuditService 
{
    @Autowired
    private MerchantAuditMapper merchantAuditMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AccountMapper accountMapper;

    private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @Override
    public MerchantAudit selectMerchantAuditById(Long id)
    {
        return merchantAuditMapper.selectMerchantAuditById(id);
    }

    @Override
    public List<MerchantAudit> selectMerchantAuditList(MerchantAudit merchantAudit)
    {
        return merchantAuditMapper.selectMerchantAuditList(merchantAudit);
    }

    @Override
    public int insertMerchantAudit(MerchantAudit merchantAudit)
    {
        merchantAudit.setStatue(1); // 默认待审核
        merchantAudit.setCreateTime(DateUtils.getNowDate());
        return merchantAuditMapper.insertMerchantAudit(merchantAudit);
    }

    @Override
    public int updateMerchantAudit(MerchantAudit merchantAudit)
    {
        merchantAudit.setUpdateTime(DateUtils.getNowDate());
        return merchantAuditMapper.updateMerchantAudit(merchantAudit);
    }

    /**
     * 审核商家
     * statue: 1待审核、2已通过、3已拒绝
     */
    @Override
    @Transactional
    public void checkMerchantAudit(Long id, Integer statue, String reason)
    {
        MerchantAudit audit = merchantAuditMapper.selectMerchantAuditById(id);
        if (audit == null)
        {
            throw new ServiceException("商家审核记录不存在");
        }

        // 更新审核状态
        audit.setStatue(statue);
        audit.setReason(reason);
        audit.setUpdateTime(new Date());
        merchantAuditMapper.updateMerchantAudit(audit);

        // 如果审核通过，创建商家用户
        if (statue == 2)
        {
            // 检查是否已创建过商家用户
            User existUser = userMapper.selectUserByPhone(audit.getPhone());
            if (existUser != null && existUser.getType() == 3)
            {
                throw new ServiceException("该手机号已注册为商家");
            }

            // 创建商家用户
            User user = new User();
            user.setName(audit.getName());
            user.setType(3); // 商家
            user.setPhone(audit.getPhone());
            user.setPassword(passwordEncoder.encode("123456")); // 默认密码
            user.setIdNo(audit.getIdNo());
            user.setAddress(audit.getAddress());
            user.setIdCardUrl(audit.getIdCardUrl());
            user.setIdCardBackUrl(audit.getIdCardBackUrl());
            user.setCertificateUrl(audit.getCertificateUrl());
            user.setHygienicUrl(audit.getHygienicUrl());
            user.setLockNum(0);
            user.setLockNumLimit(audit.getLockNumLimit());
            user.setSalesmanNo(audit.getSalesmanNo());
            user.setRegionId(audit.getRegionId());
            user.setDeleted(0);
            user.setCreateTime(new Date());
            user.setCreateBy(audit.getCreateBy());
            userMapper.insertUser(user);

            // 创建账户
            Account account = new Account();
            account.setUserId(user.getId());
            account.setBalance(0L);
            account.setTotalBalance(0L);
            account.setPeriod(DateUtils.dateTime(new Date()));
            account.setDeleted(0);
            account.setCreateTime(new Date());
            account.setCreateBy(audit.getCreateBy());
            accountMapper.insertAccount(account);
        }
    }

    @Override
    public int deleteMerchantAuditById(Long id)
    {
        return merchantAuditMapper.deleteMerchantAuditById(id);
    }
}

